<template>
	<el-dialog title="设置上架状态" :visible.sync="dialogVisible" width="700px" v-drag2anywhere>
		<el-form :model="data" label-width="100px" :rules="rules" ref="formData">
			<el-row>
				<el-col>
					<el-form-item label="原申请标题:">
						<span>{{data.pretitle}}</span>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col>
					<el-form-item label="商品编码:" style="width: 360px;" prop="sn">
						<el-input v-model="data.sn" placeholder="商品编码" :disabled="locksn"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col>
					<el-form-item label="上架渠道:" style="width: 400px;" prop="platform">
						<el-select v-model="data.platform" placeholder="上架渠道" :disabled="lockplatform" @change="handlePlatformChanged">
							<el-option v-for="(item,index) in platforms" :label="item.label" :key="item.index"
								:value="item.label" />
						</el-select>
					</el-form-item>

				</el-col>
			</el-row>
			<el-row>
				<el-col>
					<el-form-item label="上架标题:"  prop="title">
						<el-input v-model="data.title" placeholder="上架成功后的正式名称" :disabled="locktitle"></el-input>
					</el-form-item>
				</el-col>
			</el-row>

			<el-form-item label="选择状态:">
				<el-radio label="1" v-model="data.status">已提交</el-radio>
				<el-radio label="2" v-model="data.status">审核中</el-radio>
        <el-radio label="5" v-model="data.status">待上柜</el-radio>
				<el-radio label="3" v-model="data.status">上架成功</el-radio>
				<el-radio label="4" v-model="data.status">驳回</el-radio>
			</el-form-item>

			<el-form-item label="同步到京东产品库:" v-if="false">
				<el-radio label="1" v-model="async">是</el-radio>
				<el-radio label="0" v-model="async">否</el-radio>
			</el-form-item>
      <el-form-item label="驳回意见" v-if="data.status==4">
          <el-input v-model="data.errmsg" placeholder="驳回意见"></el-input>
      </el-form-item>
		</el-form>

		<div slot="footer" class="dialog-footer">
			<el-button @click="dialogVisible = false" size="medium">取 消</el-button>
			<el-button type="primary" size="medium" @click="handleSave">保存设置</el-button>
		</div>
	</el-dialog>
</template>

<script>
	export default {
		data() {
			return {
				index: -1,
				async: 0,
				data: {
					sn: '',
					platformid: "-1",
					platform: '',
					status: '-1',
					title:'111',
          errmsg:''
				},
				platforms: [],
				dialogVisible: false,
				rules: {},
				locksn: false,
				lockplatform: false,
				locktitle:false
			}
		},
		watch: {
			"data.status": function(val) {
				switch (val + "") {
					case "1":
						this.rules = {};
						this.locksn = this.lockplatform = this.locktitle=true;
						console.log('111');
						break;
					case "2":
						this.rules = {
							platform: [{
								required: true,
								message: "审核状态必须填写上架渠道!"
							}]
						};
						this.locksn = this.locktitle=true;
						this.lockplatform = false;

						break;
					case "3":
						this.rules = {
							sn: [{
								required: true,
								message: "成功上架后，请填写京东的商品编码!"
							}],
							platform: [{
								required: true,
								message: "审核状态必须填写上架渠道!"
							}],
							title: [{
								required: true,
								message: "成功上架后，请填写对应商品编码的商品名称!"
							}]
						};
						this.locksn=this.locktitle = false;
						this.lockplatform = true;
						break;
					case "4":
						this.rules = {};
						this.locksn = this.locktitle=true;
						this.lockplatform = true;
						break;

				}

			}
		},
		methods: {
			init(index, data, platforms) {
				this.index = index;
				this.data = JSON.parse(JSON.stringify(data));
				this.platforms = JSON.parse(JSON.stringify(platforms));
				this.platforms.shift();
				this.data.status = this.data.status + "";
			},
			handlePlatformChanged:function(val){
				 var option = this.platforms.filter(function(cur, index, a) {
				   if (cur.label == val) {
				     return cur;
				   }
				 })[0];
				 this.data.platformid=option.id;
				 this.data.platform=option.label;
			},
			handleSave: function() {

				this.$refs["formData"].validate(ok=>{
					if(ok){
						this.axios.post("api/admin/productnew/update_status", {
							id: this.data.id,
							status: this.data.status,
							platform:this.data.platform,
							platformid:this.data.platformid,
							title:this.data.title.trim(),
							sn:this.data.sn.trim(),
              errmsg:this.data.errmsg.trim()
						}).then((res) => {
							if (res.status == 200 && res.data.code == 0) {
								this.$message.success(res.data.msg, "提示");
								this.$emit("closeHandler", this.index, this.data);
								this.dialogVisible = false;
							} else {
								this.$message.error(res.data.msg);
							}
						})
					}
				})

			}
		}
	}
</script>

<style>
</style>
